package org.mall.modules.promotion.controller.admins;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.mall.common.api.vo.CommonResult;
import org.mall.common.api.vo.Result;
import org.mall.common.system.util.JwtUtil;
import org.mall.common.util.ObjectUtils;
import org.mall.convert.promotion.admin.BannerConvert;
import org.mall.modules.pojo.promotion.admins.AdminsBannerPageVO;
import org.mall.modules.pojo.promotion.admins.AdminsBannerVO;
import org.mall.modules.pojo.promotion.bo.BannerPageBO;
import org.mall.modules.pojo.promotion.dto.BannerAddDTO;
import org.mall.modules.pojo.promotion.dto.BannerPageDTO;
import org.mall.modules.pojo.promotion.dto.BannerUpdateDTO;
import org.mall.modules.promotion.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static org.mall.common.api.vo.CommonResult.success;

@RestController
@RequestMapping("/admins/banner")
@Api("Banner 模块")
public class AdminsBannerController {

   @Autowired
    private BannerService bannerService;

    @GetMapping("/page")
    @ApiOperation(value = "Banner 分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题，模糊匹配", example = "活动 A"),
            @ApiImplicitParam(name = "pageNo", value = "页码，从 1 开始", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, example = "10"),
    })
    public Result<?> page(@RequestParam(value = "title", required = false) String title,
                          @RequestParam(value = "pageNo", defaultValue = "0") Integer pageNo,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        BannerPageBO result = bannerService.getBannerPage(new BannerPageDTO().setTitle(title).setPageNo(pageNo).setPageSize(pageSize));
        AdminsBannerPageVO vo= BannerConvert.ADMINS.convert3(result);
        Page page=new Page();
        page.setTotal(vo.getTotal());
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        page.setRecords(vo.getList());
        return Result.ok(page);
    }

    @PostMapping("/add")
    @ApiOperation(value = "创建 Banner")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题", required = true, example = "活动A"),
            @ApiImplicitParam(name = "url", value = "跳转链接", required = true, example = "http://www.iocoder.cn"),
            @ApiImplicitParam(name = "picUrl", value = "图片链接", required = true, example = "http://www.iocoder.cn/01.jpg"),
            @ApiImplicitParam(name = "sort", value = "排序", required = true, example = "10"),
            @ApiImplicitParam(name = "memo", value = "备注", required = true, example = "活动很牛逼"),
    })
    public CommonResult<AdminsBannerVO> add(@RequestBody AdminsBannerVO adminsBannerVO) {
        BannerAddDTO bannerAddDTO= ObjectUtils.copyObject(adminsBannerVO,BannerAddDTO.class);
        return success(BannerConvert.ADMINS.convert(bannerService.addBanner(JwtUtil.getUserIdByToken(), bannerAddDTO)));
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新 Banner")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "Banner 编号", required = true, example = "1"),
            @ApiImplicitParam(name = "url", value = "跳转链接", required = true, example = "http://www.iocoder.cn"),
            @ApiImplicitParam(name = "picUrl", value = "图片链接", required = true, example = "http://www.iocoder.cn/01.jpg"),
            @ApiImplicitParam(name = "sort", value = "排序", required = true, example = "10"),
            @ApiImplicitParam(name = "memo", value = "备注", required = true, example = "活动很牛逼"),
    })
    public CommonResult<Boolean> update(@RequestBody AdminsBannerVO adminsBannerVO) {
        BannerUpdateDTO bannerUpdateDTO= ObjectUtils.copyObject(adminsBannerVO, BannerUpdateDTO.class);

        return success(bannerService.updateBanner(JwtUtil.getUserIdByToken(), bannerUpdateDTO));
    }

    @PostMapping("/update_status")
    @ApiOperation(value = "更新 Banner 状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "Banner 编号", required = true, example = "1"),
            @ApiImplicitParam(name = "status", value = "状态。1 - 开启；2 - 禁用", required = true, example = "1"),
    })
    public CommonResult<Boolean> updateStatus(@RequestParam("id") Integer id,
                                              @RequestParam("status") Integer status) {
        return success(bannerService.updateBannerStatus(JwtUtil.getUserIdByToken(), id, status));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除 Banner")
    @ApiImplicitParam(name = "id", value = "Banner 编号", required = true, example = "1")
    public CommonResult<Boolean> delete(@RequestParam("id") Integer id) {
        return success(bannerService.deleteBanner(JwtUtil.getUserIdByToken(), id));
    }

}
